package lq.cloud.studyserversystem.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import lq.cloud.studyserversystem.service.IUserService;
import lq.common.entity.QueryRequest;
import lq.common.entity.StudyResponse;
import lq.common.entity.system.SystemUser;
import lq.common.exception.StudyException;
import lq.common.utils.StudyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @program: cloud-study
 * @description: 用户管理控制器
 * @author: LQ
 * @create: 2021-02-03 17:28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping
    @PreAuthorize("hasAnyAuthority('user:view')")
    public StudyResponse userList(QueryRequest queryRequest, SystemUser user) {
        Map<String, Object> dataTable = StudyUtil.getDataTable(userService.findUserDetail(user, queryRequest));
        return new StudyResponse().data(dataTable);
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('user:add')")
    public void addUser(@Valid SystemUser user) throws StudyException {
        try {
            this.userService.createUser(user);
        } catch (Exception e) {
            String message = "新增用户失败";
            log.error(message, e);
            throw new StudyException(message);
        }
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('user:update')")
    public StudyResponse updateUser(@Valid SystemUser user) throws StudyException {
        try {
            this.userService.updateUser(user);
            return new StudyResponse().message("修改用户成功");
        } catch (Exception e) {
            String message = "修改用户失败";
            log.error(message, e);
            throw new StudyException(message);
        }
    }

    /**
     * 注解使用说明 @Valid对应实体对象传参时的参数校验；@Validated对应普通参数的校验。
     * @NotBlank (message = "{required}")注解标注了userIds参数,该注解表示请求参数不能为空，
     * 提示信息为{required}占位符里的内容。@Validated的作用是让@NotBlank注解生效。
     * {required}提示内容需要在febs-server-system模块的resources路径下创建
     * ValidationMessages.properties配置文件，在该配置文件里添加如下内容：
     * 内容为中文转Unicode的编码
     * @param userIds
     * @throws StudyException
     */
    @DeleteMapping("/{userIds}")
    @PreAuthorize("hasAnyAuthority('user:delete')")
    public StudyResponse deleteUsers(@NotBlank(message = "{required}") @PathVariable String userIds) throws StudyException {
        try {
            String[] ids = userIds.split(StringPool.COMMA);
            this.userService.deleteUsers(ids);
            return new StudyResponse().message("删除成功");
        } catch (Exception e) {
            String message = "删除用户失败";
            log.error(message, e);
            throw new StudyException(message);
        }
    }
}
